/*
 * Copyright (c) 2007 The University of Reading
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University of Reading, nor the names of the
 *    authors or contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package uk.ac.rdg.resc.wms2kml;

import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.load.Commit;

/**
 * Abstract superclass of all types of WMSCapabilities information
 * 
 * @author Jon Blower
 * $Revision$
 * $Date$
 * $Log$
 */
public abstract class WMSCapabilities
{
    /**
     * The image formats that Google Earth supports, in descending
     * order of preference
     * @todo add all formats
     */
    private static final String[] GOOGLE_EARTH_IMAGE_FORMATS = 
        new String[]{"image/png", "image/gif", "image/jpeg", "image/jpg"};
    
    private List<Layer> displayableLayers = new ArrayList<Layer>();
    private int numGroundOverlays = 0;
    
    /**
     * @return the version of the capabilities document
     */
    public abstract String getVersion();
    
    /**
     * @return the title of the WMS server
     */
    public abstract String getServerTitle();
    
    /**
     * @return a textual description of the WMS server
     */
    public abstract String getServerDescription();
    
    /**
     * @return the root Layer of this server
     */
    public abstract Layer getRootLayer();
    
    /**
     * @return the base URL (no query string parameters) for the GetMap
     * operation, <i>including the trailing question mark</i>
     */
    public abstract String getGetMapUrl();
    
    /**
     * @return the string to use to specify linear lat-lon projection for
     * this WMS version (e.g. "CRS=CRS:84" or "SRS=EPSG:4326")
     */
    public abstract String getLatLonProjectionCode();
    
    /**
     * @return an image format that is compatible with Google Earth and 
     * can be served by this WMS server
     * @throws Exception if this WMS server cannot support a format that
     * is supported by Google Earth
     */
    public abstract String getCompatibleImageFormat();
    
    /**
     * Counts the total number of layers in the WMS and the total number
     * of potential ground overlays (taking into account all the permutations
     * of depth and time dimensions).
     */
    @Commit
    public void commit()
    {
        this.countLayers(this.getRootLayer());
    }
    
    /**
     * Counts the layers and overlays starting from the given layer.
     */
    private void countLayers(Layer layer)
    {
        // Count this layer first
        if (layer.getName() != null)
        {
            this.displayableLayers.add(layer);
            // Now count all of its possible overlays
            this.numGroundOverlays += layer.getNumOverlays();
        }
        // Recursively call this method for all children
        for (Layer child : layer.getChildren())
        {
            this.countLayers(child);
        }
    }
    
    /**
     * @return a (non-hierarchical) List of all the displayable Layers
     */
    public List<Layer> getDisplayableLayers()
    {
        return this.displayableLayers;
    }
    
    /**
     * Find and return the Layer with the given unique name, or null if not found
     */
    public Layer findLayerByName(String name)
    {
        for (Layer layer : this.displayableLayers)
        {
            if (layer.getName().equals(name))
            {
                return layer;
            }
        }
        return null;
    }
    
    /**
     * @return the total number of displayable layers
     */
    public int getNumDisplayableLayers()
    {
        return this.displayableLayers.size();
    }
    
    /**
     * @return the total number of possible overlays, taking into account
     * that each layer will have an overlay for each combination of its
     * possible elevation and time values
     */
    public int getNumOverlays()
    {
        return this.numGroundOverlays;
    }
    
    /**
     * Looks through the given List of formats that are supported by the WMS,
     * trying to find a format that is also supported by Google Earth.
     * @return a supported format, or null if this WMS does not support any of
     * the formats that Google Earth understands
     */
    protected String findCompatibleImageFormat(List<String> supportedFormats)
    {
        for (String googleEarthFormat : GOOGLE_EARTH_IMAGE_FORMATS)
        {
            if (supportedFormats.contains(googleEarthFormat))
            {
                return googleEarthFormat;
            }
        }
        return null;
    }
}
